import enum
import pygame


class WalkDir(enum.IntEnum):
    down = 0
    left = 1
    right = 2
    up = 3


class DirWalkByAImage:
    """
    4个方向的行走 下 左 右 上
    """
    image_count = 16
    image_col = 4
    image_width = 49
    image_height = 74
    walk_dir = WalkDir.down

    def __init__(self, path_name: str, image_width: int, image_height: int, is_loop: bool):
        self.path_name = path_name
        self.image_index = 0
        self.action_images = []  # 四个方向的图片数组
        self.image_height = image_height
        self.image_width = image_width
        self.is_loop = is_loop
        self.image = pygame.image.load(self.path_name)
        for i in range(0, self.image_count):
            row = int(i / self.image_col)
            col = i % self.image_col
            if col == 0:
                dir = []
                self.action_images.append(dir)
            rect = (col * self.image_width,
                    row * self.image_height,
                    self.image_width,
                    self.image_height)
            image = self.image.subsurface(rect)
            dir.append(image)

    def get_image(self) -> pygame.Surface:
        """
        获取当前显示图片
        :return:
        """
        current_image = self.action_images[self.walk_dir][self.image_index]
        self.image_index += 1
        if self.image_index >= self.image_col:
            if self.is_loop:
                self.image_index = 0
            else:
                self.image_index = self.image_col - 1
        return current_image

    def set_dir(self, dir: WalkDir):
        """
        设置方向
        :param dir:  方向
        :return:
        """
        self.walk_dir = dir

    def is_end(self) -> bool:
        """
        是否播放完毕
        :return:
        """
        if self.is_loop:
            return False
        else:
            if self.image_index >= self.image_col - 1:
                return True
            else:
                return False
